package jdbc;

import java.sql.*;
import java.util.Date;
import java.util.Scanner;

public class LimitSelect {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url="jdbc:mysql://localhost:3306/java76?useSSL=false";
        Scanner sc=new Scanner(System.in);
        int startpage=0;
        int pagelength=0;
        while(startpage<0||pagelength<0){
            System.out.println("请输入开始的页数和每页长度");
            startpage=sc.nextInt();
            pagelength=sc.nextInt();
        }
        int start=(startpage-1)*pagelength;
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            con=DriverManager.getConnection(url,"root","root1234");
            String sql="select stuno,sname as 'xingming',sex,gradeid,birthday from s_student limit ?,?";
            ps=con.prepareStatement(sql);
            ps.setInt(1,start);
            ps.setInt(2,pagelength);
            rs=ps.executeQuery();
            while(rs.next()){
                int stuno=rs.getInt("stuno");
                String sname=rs.getString("xingming");
                String sex=rs.getString("sex");
                long gradeid=rs.getLong("gradeid");
                Date birthday=rs.getDate("birthday");
                System.out.println(stuno+","+sname+","+sex+","+gradeid+","+birthday);
                System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
